﻿namespace SAD.ShowNodeClientLib
{
    using System;
    using SAD.ClientLogic;
    using SAD.Common.Models;

    public class PullInfoReceiver
    {
        private int m_PullSpanTime = 1000;

        private InfoReceiverManager m_InfoReceiverManager;

        private System.Threading.Thread m_PullInfoThread;

        public void Init(InfoReceiverManager irm)
        {
            m_InfoReceiverManager = irm;
        }

        public void Start()
        {
            m_PullInfoThread = new System.Threading.Thread(new System.Threading.ThreadStart(PullInfo));
            m_PullInfoThread.Start();
        }

        public void Stop()
        {
            m_PullInfoThread.Abort();
            m_PullInfoThread = null;
        }

        private void PullInfo()
        {
            while (true)
            {
                Info_Data[] infos = MainActivator.Current.InfoManager.GetInfos(MainActivator.Current.ShowNodeData.ID, 0, m_InfoReceiverManager.LastInfoTime, 10);

                foreach (Info_Data info in infos)
                {
                    m_InfoReceiverManager.OnAddInfoEvent(info);
                }

                System.Threading.Thread.Sleep(m_PullSpanTime);
            }
        }
    }
}
